package com.kmproject.haifu.mvp.ui.adapter;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DataHelper;
import com.jess.arms.widget.alertview.AlertView;
import com.jess.arms.widget.alertview.AlertViewOnItemClickListener;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.app.utils.DataCleanManager;
import com.kmproject.haifu.mvp.model.entity.beans.MemberCenterUserInfoBean;
import com.kmproject.haifu.mvp.model.entity.beans.MyStoryBean;
import com.kmproject.haifu.mvp.model.entity.beans.UserBean;
import com.kmproject.haifu.mvp.presenter.MyStorySelectNewPresenter;
import com.kmproject.haifu.mvp.ui.activity.MemberCenterUpdateLevelActivity;
import com.kmproject.haifu.mvp.ui.activity.MyStoreActivity;
import com.kmproject.haifu.mvp.ui.activity.MyStoreSecondHandActivity;
import com.kmproject.haifu.mvp.ui.activity.MyStorySelectNewActivity;
import com.kmproject.haifu.mvp.ui.activity.SecondHandInfoShowActivity;
import com.kmproject.haifu.mvp.ui.activity.UserSetingActivity;
import com.kmproject.haifu.mvp.ui.widget.RoundCornerImageView;

import java.util.List;

public class MyStoryXZDPItemAdapter extends BaseQuickAdapter<MyStoryBean.ResultBean, BaseViewHolder> {
    ImageLoader glideImage;
    MemberCenterUserInfoBean mmemberCenterUserInfoBean;
    String mstoreType;
    private boolean isshowCheckBox;//是否显示选择框
    private MyStorySelectNewPresenter mmyStorySelectNewPresenter;
    public MyStoryXZDPItemAdapter(String storeType,MemberCenterUserInfoBean memberCenterUserInfoBean,@Nullable List<MyStoryBean.ResultBean> data, boolean misshowCheckBox,MyStorySelectNewPresenter myStorySelectNewPresenter) {
        super(R.layout.mystore_selectstory_item_item,data);
        mmemberCenterUserInfoBean=memberCenterUserInfoBean;
        mstoreType=storeType;
        isshowCheckBox=misshowCheckBox;
        mmyStorySelectNewPresenter=myStorySelectNewPresenter;
    }

    @Override
    protected void convert(BaseViewHolder helper, MyStoryBean.ResultBean item) {
        if (glideImage == null) {
            glideImage = ArmsUtils.obtainAppComponentFromContext(mContext).imageLoader();
        }
       helper.<TextView>getView(R.id.item_item_dpName_txt).setText(item.getStoreName());
        glideImage.loadImage(mContext, ImageConfigImpl.builder().url(item.getStoreLogo()).imageRadius(5)
                .imageView(helper.<RoundCornerImageView>getView(R.id.selectstory_item_item_dpHead_img))
                .build());
        if (item.getReviewStatus()==1){
            //0-未审核，1-审核中
            helper.<TextView>getView(R.id.item_item_onLine_txt).setVisibility(View.GONE);
            helper.<TextView>getView(R.id.item_item_dpJb_txt).setVisibility(View.GONE);
//            helper.<ImageView>getView(R.id.item_item_dpJb_right_icon).setVisibility(View.GONE);
            helper.<ImageView>getView(R.id.item_item_hydj_img).setVisibility(View.GONE);
            helper.<TextView>getView(R.id.item_item_dplx_txt).setVisibility(View.VISIBLE);
            helper.<TextView>getView(R.id.item_item_dplx_txt).setText("店铺类型："+mstoreType);
            helper.<TextView>getView(R.id.item_item_dpStatus_txt).setText(ArmsUtils.getString(mContext,R.string.mystory_shz));
            helper.<TextView>getView(R.id.item_item_dpStatus_txt).setTextColor(ArmsUtils.getColor(mContext,R.color.color_FF3A3A));

        }
            else if (item.getReviewStatus()==3){
            //审核未通过的
            helper.<TextView>getView(R.id.item_item_dpJb_txt).setVisibility(View.GONE);
            helper.<TextView>getView(R.id.item_item_dpStatus_txt).setText(ArmsUtils.getString(mContext,R.string.mystory_shwtg));
            helper.<TextView>getView(R.id.item_item_dpStatus_txt).setTextColor(ArmsUtils.getColor(mContext,R.color.color_FF3A3A));
            Drawable drawable= mContext.getResources().getDrawable(R.drawable.mystory_selectstory_yw_icon);
            // 这一步必须要做,否则不会显示.
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            helper.<TextView>getView(R.id.item_item_dpStatus_txt).setCompoundDrawables(null,null,drawable,null);
            helper.<TextView>getView(R.id.item_item_del_txt).setVisibility(View.VISIBLE);
            helper.<TextView>getView(R.id.item_item_onLine_txt).setVisibility(View.GONE);
        }else  if (item.getIsShow()==0||item.getReviewStatus()==2){
           //已关闭和审核过的 ，2-审核通过
           helper.<TextView>getView(R.id.item_item_onLine_txt).setVisibility(View.VISIBLE);
           helper.<TextView>getView(R.id.item_item_dpJb_txt).setText(item.getStoreRankName());
           glideImage.loadImage(mContext, ImageConfigImpl.builder().url(mmemberCenterUserInfoBean.getMemberLevel().getHeadImage()).imageRadius(5)
                   .imageView(helper.<ImageView>getView(R.id.item_item_hydj_img))
                   .build());
           helper.<TextView>getView(R.id.item_item_dplx_txt).setVisibility(View.GONE);
           if (item.getPublishInfoType()==0)
           {
               helper.<TextView>getView(R.id.item_item_onLine_txt).setText(ArmsUtils.getString(mContext,R.string.mystory_zxfy)+" "+item.getOnlineCount());
           }else if (item.getPublishInfoType()==1){
               helper.<TextView>getView(R.id.item_item_onLine_txt).setText(ArmsUtils.getString(mContext,R.string.mystory_zxwp)+" "+item.getOnlineCount());
           }else
           {
               helper.<TextView>getView(R.id.item_item_onLine_txt).setText(ArmsUtils.getString(mContext,R.string.mystory_zxpw)+" "+item.getOnlineCount());
           }
          if(item.getIsShow()==0)
          {
              //已关闭
              helper.<TextView>getView(R.id.item_item_dpStatus_txt).setText(ArmsUtils.getString(mContext,R.string.mystory_ygb));
              helper.<TextView>getView(R.id.item_item_dpStatus_txt).setTextColor(ArmsUtils.getColor(mContext,R.color.color_999999));
//              helper.<TextView>getView(R.id.item_item_del_txt).setVisibility(View.VISIBLE);
          }
              else if (item.getReviewStatus()==2)
           {
             helper.<TextView>getView(R.id.item_item_dpStatus_txt).setText(ArmsUtils.getString(mContext,R.string.mystory_jyz));
               helper.<TextView>getView(R.id.item_item_dpStatus_txt).setTextColor(ArmsUtils.getColor(mContext,R.color.color_3cb035));
           }
       }else {
           //0-未审核，1-审核中
            helper.<TextView>getView(R.id.item_item_onLine_txt).setVisibility(View.GONE);
           helper.<TextView>getView(R.id.item_item_dpJb_txt).setVisibility(View.GONE);
//           helper.<ImageView>getView(R.id.item_item_dpJb_right_icon).setVisibility(View.GONE);
           helper.<ImageView>getView(R.id.item_item_hydj_img).setVisibility(View.GONE);
           helper.<TextView>getView(R.id.item_item_dplx_txt).setVisibility(View.VISIBLE);
           helper.<TextView>getView(R.id.item_item_dplx_txt).setText("店铺类型："+mstoreType);
            helper.<TextView>getView(R.id.item_item_dpStatus_txt).setText(ArmsUtils.getString(mContext,R.string.mystory_wsh));
       }
       if (helper.getPosition()==getData().size()-1)
       {
           helper.getView(R.id.item_item_view).setVisibility(View.GONE);
       }
       helper.<TextView>getView(R.id.item_item_del_txt).setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               //删除
               if (mmyStorySelectNewPresenter!=null)
               {
                   mmyStorySelectNewPresenter.removeStore(item.getStoreId());
               }

           }
       });
    if (item.getReviewStatus()!=3)
    {
        helper.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isshowCheckBox)
                {
                    return;
                }
                if (item.getIsShow()==0&&item.getReviewStatus()==2)
                {
                    if (mmemberCenterUserInfoBean.getMemberLevel().getLevelType()>0&&mmemberCenterUserInfoBean.getStoreCount()>0)
                    {
                        new AlertView("", "您的会员已到期，所有店铺已关闭，店铺宝贝不再展示，如需继续经营店铺请升级会员等级！", "我知道了", new String[]{"去升级会员"}, null, mContext, AlertView.Style.Alert, new AlertViewOnItemClickListener() {

                            @Override
                            public void onItemClick(Object o, int position) {
                                if (position==AlertView.CANCELPOSITION)
                                {

                                }else {
                                    Intent intent = new Intent(mContext, MemberCenterUpdateLevelActivity.class);
                                    intent.putExtra("MemberCenterUserInfoBean", mmemberCenterUserInfoBean);
                                    ArmsUtils.startActivityForResult(intent, 1002);
                                }

                            }
                        },ArmsUtils.getColor(mContext,R.color.alert_button_00adfcr),true).show();
                        return;
                    }

                    if (mmemberCenterUserInfoBean.getResidualTimeDay()<1)
                    {
                        new AlertView("", "您的会员已到期，所有店铺已关闭，店铺宝贝不再展示，如需继续经营店铺请升级会员等级！", "我知道了", new String[]{"去升级会员"}, null, mContext, AlertView.Style.Alert, new AlertViewOnItemClickListener() {

                            @Override
                            public void onItemClick(Object o, int position) {
                                if (position==AlertView.CANCELPOSITION)
                                {

                                }else {
                                    Intent intent = new Intent(mContext, MemberCenterUpdateLevelActivity.class);
                                    intent.putExtra("MemberCenterUserInfoBean", mmemberCenterUserInfoBean);
                                    ArmsUtils.startActivityForResult(intent, 1002);
                                }

                            }
                        },ArmsUtils.getColor(mContext,R.color.alert_button_00adfcr),true).show();
                        return;
                    }
                    //店铺已关闭
                    ArmsUtils.makeText(mContext,"店铺已关闭");

                } else  if (item.getReviewStatus()==1)
                {
                    new AlertView("", "店铺正在审核中，审核时间约为3-5个工作日，请耐心等待！", "我知道了", null, null, mContext, AlertView.Style.Alert, new AlertViewOnItemClickListener() {

                        @Override
                        public void onItemClick(Object o, int position) {

                        }
                    },ArmsUtils.getColor(mContext,R.color.alert_button_00adfcr),true).show();
                }else if (item.getReviewStatus()==3)
                {
                    //审核未通过提示
                    new AlertView("审核未通过原因", item.getReviewDetail(), "关闭", null, null, mContext, AlertView.Style.Alert, new AlertViewOnItemClickListener() {

                        @Override
                        public void onItemClick(Object o, int position) {

                        }
                    },ArmsUtils.getColor(mContext,R.color.alert_button_00adfcr),false).show();
                }else if (item.getReviewStatus()==0)
                {
                    //未开始审核
                    new AlertView("", "店铺未被审核，请耐心等待！", "我知道了", null, null, mContext, AlertView.Style.Alert, new AlertViewOnItemClickListener() {

                        @Override
                        public void onItemClick(Object o, int position) {

                        }
                    },ArmsUtils.getColor(mContext,R.color.alert_button_00adfcr),true).show();
                }else if (item.getReviewStatus()==2){
                    if (item.getPublishInfoType()==1){
                        //二手市场
                        UserBean userBean = DataHelper.getDeviceData(mContext, AppConstant.USER_INFO);//获取用户信息
                        Intent intent=new Intent(mContext, MyStoreSecondHandActivity.class);
                        intent.putExtra("userId",userBean.getUserId());
                        intent.putExtra("userStoreId",item.getStoreId());
                        mContext.startActivity(intent);
                       return;
                    }

                    //审核通过了的
                    Intent intent=new Intent(mContext, MyStoreActivity.class);
                    intent.putExtra("userStoreId",item.getStoreId());
                    intent.putExtra("MyStoryBeanResultBean",item);
                    mContext.startActivity(intent);
                }
            }
        });
    }else {
        //审核未过的
        helper.<TextView>getView(R.id.item_item_dpStatus_txt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isshowCheckBox)
                {
                    return;
                }
                //审核未通过提示
                new AlertView("审核未通过原因", item.getReviewDetail(), "关闭", null, null, mContext, AlertView.Style.Alert, new AlertViewOnItemClickListener() {

                    @Override
                    public void onItemClick(Object o, int position) {

                    }
                },ArmsUtils.getColor(mContext,R.color.alert_button_00adfcr),false).show();

            }
        });

    }
        helper.<CheckBox>getView(R.id.selectstory_item_item_check_img).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                item.setCheckSelect(isChecked);
            }
        });

        if (isshowCheckBox)
        {
            helper.<LinearLayout>getView(R.id.selectstory_item_item_check_lay).setVisibility(View.VISIBLE);
           helper.<CheckBox>getView(R.id.selectstory_item_item_check_img).setVisibility(View.VISIBLE);
            helper.<CheckBox>getView(R.id.selectstory_item_item_check_img).setChecked(item.isCheckSelect());
        }else {
            helper.<LinearLayout>getView(R.id.selectstory_item_item_check_lay).setVisibility(View.GONE);
            helper.<CheckBox>getView(R.id.selectstory_item_item_check_img).setVisibility(View.GONE);
        }

    }
}
